<template>
  <div id="home" ref="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab
      :tabItem="tabItem"
      @itemClick="itemClick"
      ref="tab1"
      class="tab"
      v-show="isShow"
    ></tab>
    <scroll
      class="content"
      ref="scroll"
      @pullingUp="loadMore()"
      @scroll="isActive"
      :pullUpLoad="isPullUpLoad"
      :probeType="probeType"
    >
      <banner :banners="banners" class="banner" @imgload="imgload()"></banner>
      <recommend :recommends="recommends"></recommend>
      <box></box>
      <tab
        :tabItem="tabItem"
        @itemClick="itemClick"
        ref="tab"
        v-show="!isShow"
      ></tab>
      <goods-list
        :goods="showGoodsList"
        class="goods-list"
        :list="2"
      ></goods-list>
    </scroll>
    <back-top @click.native="backTop()" v-if="isActived"></back-top>
  </div>
</template>

<script>
import NavBar from "common/navbar/NavBar";
import Box from "common/box/Box";
import BackTop from "content/backTop/BackTop";
import { getHomeMutidata, getHomeGoods } from "network/home";
import { Banner, Recommend, Tab } from "views/home/children";
import GoodsList from "content/goods/GoodsList";
import { NEW, POP, SELL } from "../../common/const";
import Scroll from "common/scroll/Scroll";
export default {
  name: "Home",
  components: {
    NavBar,
    Box,
    BackTop,
    Banner,
    Recommend,
    Tab,
    GoodsList,
    Scroll,
  },
  data() {
    return {
      banners: [],
      recommends: [],
      tabItem: ["流行", "新款", "精选"],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: POP,
      timer: null,
      clientHeight: document.body.clientHeight,
      tabOffsetTop: 0,
      isActived: false,
      isShow: false,
      probeType: 3,
      isPullUpLoad: true,
    };
  },
  created() {
    //请求多个数据
    this.getHomeMutidata();
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  computed: {
    showGoodsList() {
      return this.goods[this.currentType].list;
    },
  },
  methods: {
    loadMore() {
      this.getHomeGoods(this.currentType);
    },
    getHomeMutidata() {
      getHomeMutidata().then((res) => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
      });
    },
    itemClick(index) {
      switch (index) {
        case 0:
          this.currentType = POP;
          break;
        case 1:
          this.currentType = NEW;
          break;
        case 2:
          this.currentType = SELL;
          break;
      }
      (this.$refs.tab1.currentIndex = index),
        (this.$refs.tab.currentIndex = index);
    },
    imgload() {
      this.tabOffsetTop = this.$refs.tab.$el.offsetTop;
    },
    backTop() {
      this.$refs.scroll.scroll.scrollTo(0, 0, 300);
    },
    isActive(position) {
      this.isActived = -position.y > 1000;
      this.isShow = -position.y > this.tabOffsetTop - 44;
    },
  },
};
</script>

<style lang="css" scoped>
#home {
  height: 100vh;
  position: relative;
}
.home-nav {
  width: 100%;
  background-color: var(--color-tint);
  font-weight: 700;
  color: #fff;
  z-index: 9;
}

.goods-list {
  width: 100vw;
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 49px;
}
.content {
  height: calc(100% - 93px);
  overflow: hidden;
}
.tab {
  position: relative;
  width: 100%;
  z-index: 9;
}
</style>